<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="input">
    <script>
        let data = {
            name: 'Emily'
        }
        //let newData = ''
        Object.defineProperty(data, 'name', {
            get() {
                console.log('获取数据data.name被调用了');
                //return newData
                return data.name
            },
            set(val) {
                console.log('修改data.name值得时候被调用');
                console.log(val);
                //1.当数据修改得时候会触发set()回调方法得到最新的值赋值给输入框的value
                //这就是数据层影响视图层
                document.getElementById('input').value = val
                //newData = val
                data.name=val
            }
        })
        //2.当视图层发生改变 数据层也会改变
        document.getElementById('input').addEventListener('input', (e) => {
            data.name = e.target.value
        })
    </script>
</body>

</html>